﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagChina.Basic.CCT.PLC;
using HomagGroup.Base.Communication.Items;
using Proxy.Enum;
using System;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using static HomagChina.Basic.CCT.PLC.SiemensProvider;

namespace Proxy
{
    public class MachineHandle
    {
        private SearchInfoHandle Handle_Search;
        private ModifyInfoHandle Handle_Modify;
        private PlcHandle Handle_Plc;

        public MachineHandle()
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            Handle_Plc = new PlcHandle();

            Task.Factory.StartNew(() => Position_Transport("MO1_H330L"));
            Task.Factory.StartNew(() => Position_Transport("MO1_H330R"));
        }

        public void Position_Transport(string Location)
        {
            Item Item_OrderState = null;
            Item Item_Reference = null;
            Item Item_Direction = null;
            Serilog.ILogger log = null;
            do
            {
            Start:
                Thread.Sleep(1000);
                try
                {
                    if (Globle.isInfeedAreaActive)
                    {
                        switch (Location)
                        {
                            case "MO1_H330L":
                                Item_OrderState = Wholesale.Item_MO1_H330L_OrderState;
                                Item_Reference = Wholesale.Item_MO1_H330L_Reference;
                                Item_Direction = Wholesale.Item_MO1_H330L_Direction;
                                log = SerilogHandle.Logger_M1H330L;
                                break;

                            case "MO1_H330R":
                                Item_OrderState = Wholesale.Item_MO1_H330R_OrderState;
                                Item_Reference = Wholesale.Item_MO1_H330R_Reference;
                                Item_Direction = Wholesale.Item_MO1_H330R_Direction;
                                log = SerilogHandle.Logger_M1H330R;
                                break;

                            default:
                                break;
                        }

                        var orderState = (int)(Handle_Plc.Read(Item_OrderState, "Int"));
                        var Reference = Handle_Plc.Read(Item_Reference, "String").ToString();
                        if ((!orderState.Equals((int)EnumOfOrderState.Plc请求数据)
                            && !orderState.Equals((int)EnumOfOrderState.Plc完成任务))
                            || string.IsNullOrWhiteSpace(Reference))
                        {
                            goto Start;
                        }

                        log.Information("Reference :" + Reference);
                        log.Information("orderState :" + orderState);

                        switch (orderState)
                        {
                            case (int)EnumOfOrderState.Plc完成任务:
                                ResultCode WriteFlag;
                            Write2Plc:
                                WriteFlag = Handle_Plc.Write(Item_OrderState, (int)EnumOfOrderState.CCT完成任务);
                                if (WriteFlag != ResultCode.Success)
                                {
                                    goto Write2Plc;
                                }
                                Handle_Modify.ModifyTransState(Reference);
                                break;

                            case (int)EnumOfOrderState.Plc请求数据:
                                Handle_Plc.Write(Item_Direction, (int)EnumOfDirection.Forward);
                                Handle_Plc.Write(Item_OrderState, (int)EnumOfOrderState.CCT下发任务);
                                goto Start;
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message);
                }
            } while (true);
        }
    }
}